package com.bto.solar.work.utils;

import com.alibaba.fastjson2.JSON;
import com.bto.solar.work.dto.CompanyRes;
import com.bto.solar.work.dto.DepartmentRes;
import com.bto.solar.work.dto.LeaseLessorRes;
import com.bto.solar.work.dto.LeasePrjProjectProductReq;
import com.bto.solar.work.entity.Company;
import com.bto.solar.work.entity.Department;
import com.bto.solar.work.entity.LeaseLessor;
import com.bto.solar.work.entity.LeasePrjProjectProduct;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.configurationprocessor.json.JSONException;

import java.util.List;
import java.util.Map;

/**
 * @author shiyi
 */
public class DepartmentUtils {

    public static DepartmentRes copyDepartmentRes(Department department){
        DepartmentRes departmentRes = new DepartmentRes();
        departmentRes.setDepartmentId(department.getDepartmentId());
        departmentRes.setName(department.getName());
        departmentRes.setCompanyId(department.getCompanyId());
        departmentRes.setLeader(department.getLeader());
        departmentRes.setCreateTime(department.getCreateTime());
        departmentRes.setUpdateTime(department.getUpdateTime());
        departmentRes.setParentId(department.getParentId());
        return departmentRes;
    }

    public static CompanyRes copyCompanyRes(Company company){
        CompanyRes companyRes = new CompanyRes();
        companyRes.setAddress(company.getAddress());
        companyRes.setCityCode(company.getCityCode());
        companyRes.setType(company.getType());
        companyRes.setDistrictCode(company.getDistrictCode());
        companyRes.setIsDeleted(company.getIsDeleted());
        companyRes.setName(company.getName());
        companyRes.setProvinceCode(company.getProvinceCode());
        companyRes.setCompanyId(company.getCompanyId());
        companyRes.setUpdateTime(company.getUpdateTime());
        companyRes.setCreateTime(company.getCreateTime());
        return companyRes;
    }

    public static LeaseLessorRes copyLeaseLessor(LeaseLessor leaseLessor){
        LeaseLessorRes leaseLessorRes = new LeaseLessorRes();
        leaseLessorRes.setId(leaseLessor.getId());
        leaseLessorRes.setCode(leaseLessor.getCode());
        leaseLessorRes.setCreatedAt(leaseLessor.getCreateTime());
        leaseLessorRes.setCompanyName(leaseLessor.getCompanyName());
        leaseLessorRes.setAreaId(leaseLessor.getAreaId());
        leaseLessorRes.setUpdatedAt(leaseLessor.getUpdateTime());
        return leaseLessorRes;
    }

    public static LeasePrjProjectProductReq copyLeasePrjProjectProductReq(LeasePrjProjectProduct leasePrjProjectProduct) throws JSONException {
        LeasePrjProjectProductReq leasePrjProjectProductReq = new LeasePrjProjectProductReq();
//        leasePrjProjectProductReq.setId(leasePrjProjectProduct.getId());
//        leasePrjProjectProductReq.setCode(leasePrjProjectProduct.getCode());
//        leasePrjProjectProductReq.setArea(leasePrjProjectProduct.getArea());
//        leasePrjProjectProductReq.setCreatedTime(leasePrjProjectProduct.getCreatedTime());
//        leasePrjProjectProductReq.setUpdatedTime(leasePrjProjectProduct.getUpdatedTime());
//
//        leasePrjProjectProductReq.setBasicEleTime(new JSONObject(leasePrjProjectProduct.getBasicEleTime()));
//        leasePrjProjectProductReq.setDecayRate(new JSONObject(leasePrjProjectProduct.getDecayRate()));
//        leasePrjProjectProductReq.setDepositRatio(new JSONObject(leasePrjProjectProduct.getDepositRatio()));
//        leasePrjProjectProductReq.setElePrice(new JSONObject(leasePrjProjectProduct.getElePrice()));
//        leasePrjProjectProductReq.setEquipmentPrice(new JSONObject(leasePrjProjectProduct.getEquipmentPrice()));
//        leasePrjProjectProductReq.setMaintenancePrice(new JSONObject(leasePrjProjectProduct.getMaintenancePrice()));
//        leasePrjProjectProductReq.setManufacturerInconeRatio(new JSONObject(leasePrjProjectProduct.getManufacturerInconeRatio()));
//        leasePrjProjectProductReq.setResidualsRate(new JSONObject(leasePrjProjectProduct.getResidualsRate()));
//        leasePrjProjectProductReq.setTenantIncomePrice(new JSONObject(leasePrjProjectProduct.getTenantIncomePrice()));
//        leasePrjProjectProductReq.setComponent(new JSONObject(leasePrjProjectProduct.getComponent()));
        return leasePrjProjectProductReq;
    }


    public static LeasePrjProjectProduct copyLeasePrjProjectProduct(LeasePrjProjectProduct leasePrjProjectProduct) throws JsonProcessingException {
        if(leasePrjProjectProduct.getBasicEleTime() != null){
            ObjectMapper objectMapper = new ObjectMapper();
            String basicEleTime = leasePrjProjectProduct.getBasicEleTime().toString();
            //leasePrjProjectProduct.setBasicEleTime("'" + leasePrjProjectProduct.getBasicEleTime() +"'");
            Map<String, Object> jsonObject = objectMapper.readValue(basicEleTime, new TypeReference<Map<String, Object>>(){});
            leasePrjProjectProduct.setBasicEleTime(JSON.toJSONString(jsonObject));
            //leasePrjProjectProduct.setBasicEleTime(basicEleTime);
        }
        if(leasePrjProjectProduct.getDecayRate() != null){
            ObjectMapper objectMapper = new ObjectMapper();
            //leasePrjProjectProduct.setDecayRate("'" + leasePrjProjectProduct.getDecayRate() +"'");
            List<Map<String, Object>> jsonObject = objectMapper.readValue(leasePrjProjectProduct.getDecayRate().toString(), new TypeReference<List<Map<String, Object>>>(){});
            leasePrjProjectProduct.setDecayRate(JSON.toJSONString(jsonObject));
        }
        if(leasePrjProjectProduct.getDepositRatio() != null){
            ObjectMapper objectMapper = new ObjectMapper();
            //leasePrjProjectProduct.setDepositRatio("'" + leasePrjProjectProduct.getDepositRatio() +"'");
            Map<String, Object> jsonObject = objectMapper.readValue(leasePrjProjectProduct.getDepositRatio().toString(), new TypeReference<Map<String, Object>>(){});
            leasePrjProjectProduct.setDepositRatio(JSON.toJSONString(jsonObject));
        }
        if(leasePrjProjectProduct.getElePrice() != null){
            ObjectMapper objectMapper = new ObjectMapper();
            //leasePrjProjectProduct.setElePrice("'" + leasePrjProjectProduct.getElePrice() +"'");
            Map<String, Object> jsonObject = objectMapper.readValue(leasePrjProjectProduct.getElePrice().toString(), new TypeReference<Map<String, Object>>(){});
            leasePrjProjectProduct.setElePrice(JSON.toJSONString(jsonObject));
        }
        if(leasePrjProjectProduct.getEquipmentPrice() != null){
            ObjectMapper objectMapper = new ObjectMapper();
           // leasePrjProjectProduct.setEquipmentPrice("'" + leasePrjProjectProduct.getEquipmentPrice() +"'");

            Map<String, Object> jsonObject = objectMapper.readValue(leasePrjProjectProduct.getEquipmentPrice().toString(), new TypeReference<Map<String, Object>>(){});
            leasePrjProjectProduct.setEquipmentPrice(JSON.toJSONString(jsonObject));
        }
        if(leasePrjProjectProduct.getMaintenancePrice() != null){
            ObjectMapper objectMapper = new ObjectMapper();
            //leasePrjProjectProduct.setMaintenancePrice("'" + leasePrjProjectProduct.getMaintenancePrice() +"'");

            List<Map<String, Object>> jsonObject = objectMapper.readValue(leasePrjProjectProduct.getMaintenancePrice().toString(), new TypeReference<List<Map<String, Object>>>(){});
            leasePrjProjectProduct.setMaintenancePrice(JSON.toJSONString(jsonObject));
        }
        if(leasePrjProjectProduct.getManufacturerInconeRatio() != null){
            ObjectMapper objectMapper = new ObjectMapper();
           // leasePrjProjectProduct.setManufacturerInconeRatio("'" + leasePrjProjectProduct.getManufacturerInconeRatio() +"'");

            Map<String, Object> jsonObject = objectMapper.readValue(leasePrjProjectProduct.getManufacturerInconeRatio().toString(), new TypeReference<Map<String, Object>>(){});
            leasePrjProjectProduct.setManufacturerInconeRatio(JSON.toJSONString(jsonObject));
        }
        if(leasePrjProjectProduct.getResidualsRate() != null){
            ObjectMapper objectMapper = new ObjectMapper();
            //leasePrjProjectProduct.setResidualsRate("'" + leasePrjProjectProduct.getResidualsRate() +"'");

            Map<String, Object> jsonObject = objectMapper.readValue(leasePrjProjectProduct.getResidualsRate().toString(), new TypeReference<Map<String, Object>>(){});
            leasePrjProjectProduct.setResidualsRate(JSON.toJSONString(jsonObject));
        }
        if(leasePrjProjectProduct.getTenantIncomePrice() != null){
            ObjectMapper objectMapper = new ObjectMapper();
            //leasePrjProjectProduct.setTenantIncomePrice("'" + leasePrjProjectProduct.getTenantIncomePrice() +"'");

            List<Map<String, Object>> jsonObject = objectMapper.readValue(leasePrjProjectProduct.getTenantIncomePrice().toString(), new TypeReference<List<Map<String, Object>>>(){});
            leasePrjProjectProduct.setTenantIncomePrice(JSON.toJSONString(jsonObject));
        }
        if(leasePrjProjectProduct.getComponent() != null){
            ObjectMapper objectMapper = new ObjectMapper();
            //leasePrjProjectProduct.setComponent("'" + leasePrjProjectProduct.getComponent() +"'");

            Map<String, Object> jsonObject = objectMapper.readValue(leasePrjProjectProduct.getComponent().toString(), new TypeReference<Map<String, Object>>(){});
            leasePrjProjectProduct.setComponent(JSON.toJSONString(jsonObject));
        }
        return leasePrjProjectProduct;
    }

    public static LeasePrjProjectProduct saveLeasePrjProjectProduct(LeasePrjProjectProduct leasePrjProjectProduct) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        if(leasePrjProjectProduct.getBasicEleTime() != null){
            //leasePrjProjectProduct.setBasicEleTime(objectMapper.writeValueAsString(leasePrjProjectProduct.getBasicEleTime()));
            leasePrjProjectProduct.setBasicEleTime(leasePrjProjectProduct.getBasicEleTime().replace("\\" , ""));
        }
        if(leasePrjProjectProduct.getDecayRate() != null){
            //leasePrjProjectProduct.setDecayRate(objectMapper.writeValueAsString(leasePrjProjectProduct.getDecayRate()));
            leasePrjProjectProduct.setDecayRate(leasePrjProjectProduct.getDecayRate().replace("\\" , ""));
        }
        if(leasePrjProjectProduct.getDepositRatio() != null){
            //leasePrjProjectProduct.setDepositRatio(objectMapper.writeValueAsString(leasePrjProjectProduct.getDepositRatio()));
            leasePrjProjectProduct.setDepositRatio(leasePrjProjectProduct.getDepositRatio().replace("\\" , ""));
        }
        if(leasePrjProjectProduct.getElePrice() != null){
            //leasePrjProjectProduct.setElePrice(objectMapper.writeValueAsString(leasePrjProjectProduct.getElePrice()));
            leasePrjProjectProduct.setElePrice(leasePrjProjectProduct.getElePrice().replace("\\" , ""));
        }
        if(leasePrjProjectProduct.getEquipmentPrice() != null){
            //leasePrjProjectProduct.setEquipmentPrice(objectMapper.writeValueAsString(leasePrjProjectProduct.getEquipmentPrice()));
            leasePrjProjectProduct.setEquipmentPrice(leasePrjProjectProduct.getEquipmentPrice().replace("\\" , ""));
        }
        if(leasePrjProjectProduct.getMaintenancePrice() != null){
            //leasePrjProjectProduct.setMaintenancePrice(objectMapper.writeValueAsString(leasePrjProjectProduct.getMaintenancePrice()));
            leasePrjProjectProduct.setMaintenancePrice(leasePrjProjectProduct.getMaintenancePrice().replace("\\" , ""));
        }
        if(leasePrjProjectProduct.getManufacturerInconeRatio() != null){
            //leasePrjProjectProduct.setManufacturerInconeRatio(objectMapper.writeValueAsString(leasePrjProjectProduct.getManufacturerInconeRatio()));
            leasePrjProjectProduct.setManufacturerInconeRatio(leasePrjProjectProduct.getManufacturerInconeRatio().replace("\\" , ""));
        }
        if(leasePrjProjectProduct.getResidualsRate() != null){
            //leasePrjProjectProduct.setResidualsRate(objectMapper.writeValueAsString(leasePrjProjectProduct.getResidualsRate()));
            leasePrjProjectProduct.setResidualsRate(leasePrjProjectProduct.getResidualsRate().replace("\\" , ""));
        }
        if(leasePrjProjectProduct.getTenantIncomePrice() != null){
            //leasePrjProjectProduct.setTenantIncomePrice(objectMapper.writeValueAsString(leasePrjProjectProduct.getTenantIncomePrice()));
            leasePrjProjectProduct.setTenantIncomePrice(leasePrjProjectProduct.getTenantIncomePrice().replace("\\" , ""));
        }
        if(leasePrjProjectProduct.getComponent() != null){
            //leasePrjProjectProduct.setComponent(objectMapper.writeValueAsString(leasePrjProjectProduct.getComponent()));
            leasePrjProjectProduct.setComponent(leasePrjProjectProduct.getComponent().replace("\\" , ""));
        }
        return leasePrjProjectProduct;
    }

}
